<?php

class usergift extends spModel
{
	public $pk = "id"; // 数据表的主键
    public $table = "usergift"; // 数据表的名称

	function create($row) {
		$mGift = spClass('gift');
		$ret   = $mGift->find(array('id'=>$row['gid']));
		$this->runSql("UPDATE gift SET sold = sold+1 WHERE id = ".$row['gid']);

		$mUser = spClass('user');
		$mUser->runSql("UPDATE user SET cash = cash-".$ret['value']." WHERE uid = ".$row['uid']);

		$user  = $mUser->find(array('uid'=>$row['uid']));
		$mUserLog = spClass('userlog');
		$logArray	  = array(
							'uid' => $row['buyfrom'],
							'type'=> 'gift',
							'content' => 'Use '. $ret['value']. ' for a Gift ' . $ret['name'] . ' to ' . $user['nickname'],
							'dateline'=> time()
						);
		$lastLogId    = $mUserLog->create($logArray);

		$user  = $mUser->find(array('uid'=>$row['buyfrom']));
		$logArray	  = array(
							'uid' => $row['uid'],
							'type'=> 'receivegift',
							'content' => 'Receive a gift ' . $ret['name'] . ' from ' . $user['nickname'],
							'dateline'=> time()
						);

		$mUserCashLog = spClass('usercashlog');
		$cashLogArray	  = array(
							'uid' => $row['uid'],
							'logid' => $lastLogId,
							'amount'=> -$ret['value'],
							'dateline'=> time()
						);
		$mUserCashLog->create($cashLogArray);

		parent::create($row);
	}
}